package com.kystar.muxplayer.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.jaygoo.widget.VerticalRangeSeekBar;
import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;

public class BrightnessPopupWindow extends PopupWindow implements OnRangeChangedListener {

    public interface ProgressChanged {

        void onProgressed(int process);
    }

    VerticalRangeSeekBar mSeekBar;
    TextView mTextView;

    private ProgressChanged mProgressChanged;

    public BrightnessPopupWindow(Context context) {
        super(LayoutInflater.from(context).inflate(R.layout.layout_brightness_seekbar, null), 40,
            175);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        update();
        mSeekBar = getContentView().findViewById(R.id.seekbar);
        mTextView = getContentView().findViewById(R.id.text);

        mSeekBar.setOnRangeChangedListener(this);
        setProgress(0);


    }

    public void setProgress(int value) {
        progress = value;
        mSeekBar.setValue(value);
        mTextView.setText(value + "");
    }

    public void setOnProgressChanged(ProgressChanged l) {
        mProgressChanged = l;
    }

    public void show(View anchor) {
        int offset = 0;
        int[] size = Helper.measureWrapContent(getContentView());
        offset = -(anchor.getMeasuredHeight() + 175);
        showAsDropDown(anchor, 0, offset);
    }

    private int progress = 0;
    @Override
    public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue,
        boolean isFromUser) {
        progress = Math.round(leftValue);
        mTextView.setText("" + progress);
    }

    @Override
    public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

    }

    @Override
    public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
        if (mProgressChanged != null) {
            mProgressChanged.onProgressed(progress);
        }
    }
}
